﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using Comm.HardDisk;
using Microsoft.Win32;
using SmartMeterParamSetting.SchemesServer;

namespace SmartMeterParamSetting.UI
{
    /// <summary>
    ///     UI_About.xaml 的交互逻辑
    /// </summary>
    public partial class UI_About : Window
    {
        public UI_About()
        {
            InitializeComponent();


            Btn_ImportLicense.Visibility = Mod_Main.IsLicense ? Visibility.Hidden : Visibility.Visible;

            Txt_System.Text = string.Format("V {0}", Environment.Version);
            Txt_Assembly.Text = string.Format("V {0}", Application.ResourceAssembly.GetName().Version);
            Txt_FileVersion.Text = string.Format("V {0}",
                                                 FileVersionInfo.GetVersionInfo(
                                                     Assembly.GetEntryAssembly().CodeBase.Replace("file:///", ""))
                                                                .FileVersion);
            Txt_OS.Text = Environment.OSVersion.ToString();
            try
            {
                Txt_DiskSn.Text = HardDiskHelper.GetAllHardDiskSN().Split(',')[0].Trim();
            }
            catch
            {
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (((Button) sender).Name.Equals(Btn_ImportLicense.Name, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    var client = new SchemesServerClient();
                    client.Endpoint.Address = new EndpointAddress(Mod_Main.systemInfo.FaServer);

                    byte[] data = client.GetClientLicense(HardDiskHelper.GetAllHardDiskSN().Split(',')[0].Trim());

                    if (data.Length > 0)
                    {
                        File.WriteAllBytes(string.Format(@"{0}\Clicense.dat", Environment.CurrentDirectory), data);
                        MessageBox.Show("许可文件导入成功，请重新启动系统加载许可文件...", "许可导入", MessageBoxButton.OK,
                                        MessageBoxImage.Information);
                        Close();
                        return;
                    }
                }
                catch
                {
                }

                var openFile = new OpenFileDialog();

                openFile.DefaultExt = "dat";

                openFile.Filter = "(许可文件).dat|*.dat";

                openFile.FileName = "Clicense";

                openFile.Title = "导入许可文件";

                bool? result = openFile.ShowDialog();

                if (!(bool) result) return;

                try
                {
                    File.Copy(openFile.FileName, string.Format(@"{0}\Clicense.dat", Environment.CurrentDirectory), true);


                    MessageBox.Show("许可文件导入成功，请重新启动系统加载许可文件...", "许可导入", MessageBoxButton.OK,
                                    MessageBoxImage.Information);

                    Close();
                }
                catch
                {
                    MessageBox.Show("许可文件导入出现一个意外...", "许可导入", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
            }
            else
            {
                Close();
            }
        }
    }
}